home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-mail.php < prev    next >
Encoding:
PHP Script  |  2005-02-01  |  5.5 KB  |  182 lines

  1. <?php
  2. require(dirname(__FILE__) . '/wp-config.php');
  3.  
  4. require_once(ABSPATH.WPINC.'/class-pop3.php');
  5.  
  6. error_reporting(2037);
  7.  
  8. $time_difference = get_settings('gmt_offset') * 3600;
  9.  
  10. $phone_delim = '::';
  11.  
  12. $pop3 = new POP3();
  13.  
  14. if (!$pop3->connect(get_settings('mailserver_url'), get_settings('mailserver_port'))) :
  15.     echo "Ooops $pop3->ERROR <br />\n";
  16.     exit;
  17. endif;
  18.  
  19. $count = $pop3->login(get_settings('mailserver_login'), get_settings('mailserver_pass'));
  20. if (0 == $count) die(__('There doesn’t seem to be any new mail.'));
  21.  
  22.  
  23. for ($i=1; $i <= $count; $i++) :
  24.  
  25.     $message = $pop3->get($i);
  26.  
  27.     $content = '';
  28.     $content_type = '';
  29.     $boundary = '';
  30.     $bodysignal = 0;
  31.     $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  32.                      'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  33.     foreach ($message as $line) :
  34.         if (strlen($line) < 3) $bodysignal = 1;
  35.  
  36.         if ($bodysignal) {
  37.             $content .= $line;
  38.         } else {
  39.             if (preg_match('/Content-Type: /i', $line)) {
  40.                 $content_type = trim($line);
  41.                 $content_type = substr($content_type, 14, strlen($content_type)-14);
  42.                 $content_type = explode(';', $content_type);
  43.                 $content_type = $content_type[0];
  44.             }
  45.             if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
  46.                 $boundary = trim($line);
  47.                 $boundary = explode('"', $boundary);
  48.                 $boundary = $boundary[1];
  49.             }
  50.             if (preg_match('/Subject: /i', $line)) {
  51.                 $subject = trim($line);
  52.                 $subject = substr($subject, 9, strlen($subject)-9);
  53.                 if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $subject)) {
  54.                   $subject = wp_iso_descrambler($subject);
  55.                 }
  56.                 // Captures any text in the subject before $phone_delim as the subject
  57.                 $subject = explode($phone_delim, $subject);
  58.                 $subject = $subject[0];
  59.             }
  60.  
  61.             // Set the author using the email address (To or Reply-To, the last used)
  62.             // otherwise use the site admin
  63.             if (preg_match('/From: /', $line) | preg_match('Reply-To: /', $line))  {
  64.                 $author=trim($line);
  65.             if ( ereg("([a-zA-Z0-9\_\-\.]+@[\a-zA-z0-9\_\-\.]+)", $author , $regs) ) {
  66.                 echo "Author = {$regs[1]} <p>";
  67.                 $result = $wpdb->get_row("SELECT ID FROM $tableusers WHERE user_email='$regs[1]' ORDER BY ID DESC LIMIT 1");
  68.                 if (!$result)
  69.                     $post_author = 1;
  70.                 else
  71.                     $post_author = $result->ID;
  72.             } else
  73.                 $post_author = 1;
  74.             }
  75.  
  76.             if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
  77.                 $ddate = trim($line);
  78.                 $ddate = str_replace('Date: ', '', $ddate);
  79.                 if (strpos($ddate, ',')) {
  80.                     $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
  81.                 }
  82.                 $date_arr = explode(' ', $ddate);
  83.                 $date_time = explode(':', $date_arr[3]);
  84.                 
  85.                 $ddate_H = $date_time[0];
  86.                 $ddate_i = $date_time[1];
  87.                 $ddate_s = $date_time[2];
  88.                 
  89.                 $ddate_m = $date_arr[1];
  90.                 $ddate_d = $date_arr[0];
  91.                 $ddate_Y = $date_arr[2];
  92.                 for ($j=0; $j<12; $j++) {
  93.                     if ($ddate_m == $dmonths[$j]) {
  94.                         $ddate_m = $j+1;
  95.                     }
  96.                 }
  97.  
  98.                 $time_zn = intval($date_arr[4]) * 36;
  99.                 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
  100.                 $ddate_U = $ddate_U - $time_zn;
  101.                 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
  102.                 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
  103.             }
  104.         }
  105.     endforeach;
  106.  
  107.     $subject = trim(str_replace(get_settings('subjectprefix'), '', $subject));
  108.  
  109.     if ($content_type == 'multipart/alternative') {
  110.         $content = explode('--'.$boundary, $content);
  111.         $content = $content[2];
  112.         $content = explode('Content-Transfer-Encoding: quoted-printable', $content);
  113.         $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
  114.     }
  115.     $content = trim($content);
  116.     // Captures any text in the body after $phone_delim as the body
  117.     $content = explode($phone_delim, $content);
  118.     $content[1] ? $content = $content[1] : $content = $content[0];
  119.  
  120.     echo "<p><b>Content-type:</b> $content_type, <b>boundary:</b> $boundary</p>\n";
  121.     echo "<p><b>Raw content:</b><br /><pre>".$content.'</pre></p>';
  122.  
  123.     $content = trim($content);
  124.  
  125.     $post_content = apply_filters('phone_content', $content);
  126.  
  127.     $post_title = xmlrpc_getposttitle($content);
  128.  
  129.     if ($post_title == '') $post_title = $subject;
  130.  
  131.     if (empty($post_categories)) $post_categories[] = get_settings('default_email_category');
  132.  
  133.     $post_category = $post_categories;
  134.  
  135.     // or maybe we should leave the choice to email drafts? propose a way
  136.     $post_status = 'publish';
  137.  
  138.     $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
  139.  
  140.     $post_ID = wp_insert_post($post_data);
  141.  
  142.     if (!$post_ID) {
  143.         // we couldn't post, for whatever reason. better move forward to the next email
  144.         continue;
  145.     }
  146.  
  147.     do_action('publish_phone', $post_ID);
  148.  
  149.     echo "\n<p><b>Author:</b> $post_author</p>";
  150.     echo "\n<p><b>Posted title:</b> $post_title<br />";
  151.     echo "\n<b>Posted content:</b><br /><pre>".$content.'</pre></p>';
  152.  
  153.     if (!$post_categories) $post_categories[] = 1;
  154.     foreach ($post_categories as $post_category) :
  155.         $post_category = intval($post_category);
  156.  
  157.         // Double check it's not there already
  158.         $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_ID AND category_id = $post_category");
  159.  
  160.         if (!$exists && $result) { 
  161.             $wpdb->query("
  162.             INSERT INTO $wpdb->post2cat
  163.             (post_id, category_id)
  164.             VALUES
  165.             ($post_ID, $post_category)
  166.             ");
  167.         }
  168.     endforeach;
  169.  
  170.     if(!$pop3->delete($i)) {
  171.         echo '<p>Oops '.$pop3->ERROR.'</p></div>';
  172.         $pop3->reset();
  173.         exit;
  174.     } else {
  175.         echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";
  176.     }
  177.  
  178. endfor;
  179.  
  180. $pop3->quit();
  181.  
  182. ?>